//    Copyright (C) Kherty.  All rights reserved.
using System.Windows;
using System.Windows.Input;

namespace OpenLS.Spreadsheet.UI
{
    static class GridFiltering
    {
        internal static void EnableCommands(UIElement g)
        {
            g.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.ApplyFilter,
                OnApplyFilter, WorksheetHelper.OnAlways));
            g.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.ClearFilter,
                OnClearFilter, WorksheetHelper.OnAlways));
            g.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.ReApplyFilter,
                OnReApplyFilter, WorksheetHelper.OnAlways));

        }
        static void OnApplyFilter(object sender, ExecutedRoutedEventArgs args)
        {
            Worksheet worksheet = GetWorksheet(sender);
            args.Handled = true;
            if (worksheet.AutoFilter != null)
            {
                worksheet.AutoFilter = null;
                return;
            }
            worksheet.AutoFilter = new AutoFilter(worksheet.Selection);
        }
        static void OnClearFilter(object sender, ExecutedRoutedEventArgs args)
        {
            Worksheet worksheet = GetWorksheet(sender);
            worksheet.AutoFilter = null;
            args.Handled = true;
        }
        static void OnReApplyFilter(object sender, ExecutedRoutedEventArgs args)
        {
            Worksheet Worksheet = GetWorksheet(sender);
        }
        private static Worksheet GetWorksheet(object sender)
        {
            return WorksheetHelper.GetWorksheet(sender);
        }

    }
}
